//
//  BSLAlbumsTableViewCell.m
//  Demo
//
//  Created by RYAN on 15/6/23.
//
//

#import "BSLAlbumsTableViewCell.h"

@implementation BSLAlbumsTableViewCell

- (void)awakeFromNib {
    // Initialization code
    
    self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    self.selectionStyle = UITableViewCellSelectionStyleGray;
    
    [self.groupNameLabel setFont:[UIFont fontWithName:@"system" size:17]];
    [self.posterImageView setFrame:CGRectMake(0, 0, CELL_HEIGHT, CELL_HEIGHT)];
    CGRect frame = self.groupNameLabel.frame;
    self.groupNameLabel.frame = CGRectMake(CELL_HEIGHT + 5, frame.origin.y, frame.size.width, frame.size.height);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setDataWithAlbum:(ALAssetsGroup *)album{
    CGImageRef imageRef = [album posterImage];
    [self.posterImageView setImage:[UIImage imageWithCGImage:imageRef]];
    
    NSString *albumName = [album valueForProperty:ALAssetsGroupPropertyName];
    [self.groupNameLabel setText:albumName];
    
    [self.photoNumLabel setText:[NSString stringWithFormat:@"(%ld)",(long)album.numberOfAssets]];
    
    [self adjustLabels];
}

- (void)adjustLabels{
    CGFloat width;
    if ([[UIDevice currentDevice].systemVersion floatValue] >7.0) {
        CGRect rect = [self.groupNameLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, self.groupNameLabel.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading  attributes:[NSDictionary dictionaryWithObjectsAndKeys:self.groupNameLabel.font,NSFontAttributeName, nil] context:nil];
        width = rect.size.width;
    }
    
    else{
        CGSize size = [self.groupNameLabel.text sizeWithFont:self.groupNameLabel.font constrainedToSize:CGSizeMake(MAXFLOAT, self.groupNameLabel.frame.size.height) lineBreakMode:self.groupNameLabel.lineBreakMode];
        width = size.width;
    }
    width = (width > CELL_LABEL_MAX_WIDTH) ? CELL_LABEL_MAX_WIDTH : width;
    
    CGRect frame = self.groupNameLabel.frame;
    [self.groupNameLabel setFrame:CGRectMake(frame.origin.x, frame.origin.y, width, frame.size.height)];
    [self.photoNumLabel setFrame:CGRectMake(self.groupNameLabel.frame.size.width + self.groupNameLabel.frame.origin.x + 5, self.photoNumLabel.frame.origin.y, self.photoNumLabel.frame.size.width, self.groupNameLabel.frame.size.height)];

}

@end
